package com.cimr.controller.user;

import com.cimr.comm.bo.MenuBo;
import com.cimr.comm.bo.OperationBo;
import com.cimr.comm.dto.HttpResult;
import com.cimr.comm.dto.LayuiTableData;
import com.cimr.comm.service.OperationService;
import com.cimr.comm.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

/**
 * Created by zengfan on 2018/1/5.
 */
@Controller
@RequestMapping("/operation/ajax")
public class OperationManagementAjaxController {
    @Autowired
    private OperationService operationService;

    @RequestMapping({"getOperationList"})
    @ResponseBody
    public LayuiTableData getOperationList(@RequestParam(required=true,defaultValue="1") Integer page,
                                           @RequestParam(required=false,defaultValue="10") Integer limit,
                                           @RequestParam(required = false) String operationName) {
        PageHelper.startPage(page, limit);
        List<OperationBo> list = operationService.getOperationListWithName(operationName);
        PageInfo<MenuBo> pageInfo = new PageInfo(list);
        List<OperationBo> list1 = operationService.getOperationListWithName(operationName);
        LayuiTableData result = new LayuiTableData();
        result.setCode(0);
        result.setMsg("");
        result.setCount(list1.size());
        result.setData(pageInfo.getList());
        return result;
    }
    /**
    *@auther zengfan
    *@date 2018/1/10 16:07
    *@params  * @param null
    *根据id查询已经分配的操作
    **/
    @RequestMapping({"getOperationByRoleId"})
    @ResponseBody
    public HttpResult getOperationByRoleId(@RequestParam(required = true) String id) {
        List<OperationBo> currentList = operationService.getOperationListByRoleId(id);
        HttpResult result = new HttpResult(true, "");
        result.setData(currentList);
        return result;
    }
    @RequestMapping({"getOperationById"})
    @ResponseBody
    public HttpResult getOperationById(@RequestParam(required = true) String id) {
        OperationBo operationBo = operationService.getOperationById(id);
        HttpResult result = new HttpResult(true, "");
        result.setData(operationBo);
        return result;
    }
    @RequestMapping({"saveOperation"})
    @ResponseBody
    public HttpResult saveOperation(String id,String operationKey,String operationName,String comment,Float orderId){
        int resultStatus1;
        if(id != null && !"".equals(id)) {
            resultStatus1 = operationService.updateOperation(id,operationKey,operationName,comment,orderId);
        } else {
            resultStatus1 =  operationService.insertOperation(IdUtil.getId().toString(),operationKey,operationName,comment,orderId);
        }

        HttpResult result = new HttpResult(true, "");
        if(resultStatus1 == 0) {
            result.setSuccess(false);
        }
        return result;
    }
    @RequestMapping({"deleteOperation"})
    @ResponseBody
    public HttpResult deleteOperation(String id){
        int resultStatus1;
        resultStatus1 =  operationService.deleteOperationById(id);
        HttpResult result = new HttpResult(true, "");
        if(resultStatus1 == 0) {
            result.setSuccess(false);
        }
        return result;
    }
    @RequestMapping({"getOperationListByKey"})
    @ResponseBody
    public HttpResult getOperationListByKey(String operationKey) {
        HttpResult result = new HttpResult(true, "");
        List<OperationBo> list =  operationService.getOperationListByKey(operationKey);

        if(list!=null&&list.size()==0){
            result.setData(null);
        }else{
            result.setData(list);
        }
        return result;
    }
    @RequestMapping({"getOperationListByKeyOutCurrent"})
    @ResponseBody
    public HttpResult getOperationListByKeyOutCurrent(String id,String operationKey) {
        HttpResult result = new HttpResult(true, "");
        List<OperationBo> list =  operationService.getOperationListByKey(operationKey);
        if(list!=null&&list.size()!=0){
            if (list.get(0).getId().equals(id)){
                result.setData(null);
            }else {
                result.setData(list);
            }
        }else{
            result.setData(null);
        }
        return result;
    }
}
